import { Aiui } from "../static";
/**
 * 继承方法
 * @param {Object} target
 */
export default function extend(target) {
  var src,
    copyIsArray,
    copy,
    name,
    options,
    clone,
    target = arguments[0] || {},
    i = 1,
    length = arguments.length,
    deep = false;

  if (typeof target === "boolean") {
    deep = target;

    target = arguments[i] || {};
    i++;
  }
  if (typeof target !== "object" && !Aiui.isFunction(target)) {
    target = {};
  }
  if (i === length) {
    target = this;
    i--;
  }
  for (; i < length; i++) {
    if ((options = arguments[i]) != null) {
      for (name in options) {
        src = target[name];
        copy = options[name];

        if (target === copy) {
          continue;
        }
        if (
          deep &&
          copy &&
          (Aiui.isPlainObject(copy) || (copyIsArray = Aiui.isArray(copy)))
        ) {
          if (copyIsArray) {
            copyIsArray = false;
            clone = src && Aiui.isArray(src) ? src : [];
          } else {
            clone = src && Aiui.isPlainObject(src) ? src : {};
          }
          target[name] = extend(deep, clone, copy);
        } else if (copy !== undefined) {
          target[name] = copy;
        }
      }
    }
  }
  return target;
};